Running Unit Tests for Multi-git
In this lesson, you will learn how to run the unit tests we created in the previous lesson using either go test or Ginkgo. We will also check the test coverage and run focused tests, which is very useful during development.
The go test command has several other command-line arguments that can be useful. Check out the full list here: https://golang.org/cmd/go/#hdr-Testing_flags
However, when running Ginkgo tests, I recommend using the Ginkgo command itself.
Running Ginkgo tests with Ginkgo#
First, make sure that ginkgo is installed. If it’s not, follow the instructions here: https://onsi.github.io/ginkgo/#getting-ginkgo
As you can see, the context and the test name are displayed for each test.
Checking test coverage#
We can check the test coverage with Ginkgo as well using the ginkgo -cover command.
Okay. We have 70.6% coverage, but which parts of the code are not covered by tests? Let’s generate a coverage profile that we can later review:
$ ginkgo -coverprofile=coverage.out
Unfortunately, Ginkgo fails to generate the cover profile when running inside a Docker container. I reported it in the following GitHub issue: https://github.com/onsi/ginkgo/issues/767
Hopefully, the developer will fix it soon.
The coverage.out file is a simple text file with lines that are not covered:
You can review it using the following command:
$ cat coverage.out
mode: set
github.com/the-gigi/multi-git/pkg/repo_manager/repo_manager.go:16.114,18.16 2 1
github.com/the-gigi/multi-git/pkg/repo_manager/repo_manager.go:25.2,25.36 1 1
github.com/the-gigi/multi-git/pkg/repo_manager/repo_manager.go:29.2,29.25 1 1
github.com/the-gigi/multi-git/pkg/repo_manager/repo_manager.go:34.2,37.30 2 1
github.com/the-gigi/multi-git/pkg/repo_manager/repo_manager.go:42.2,42.8 1 1
github.com/the-gigi/multi-git/pkg/repo_manager/repo_manager.go:18.16,19.25 1 1
github.com/the-gigi/multi-git/pkg/repo_manager/repo_manager.go:22.3,22.9 1 1
github.com/the-gigi/multi-git/pkg/repo_manager/repo_manager.go:19.25,21.4 1 1
github.com/the-gigi/multi-git/pkg/repo_manager/repo_manager.go:25.36,27.3 1 1
github.com/the-gigi/multi-git/pkg/repo_manager/repo_manager.go:29.25,32.3 2 1
github.com/the-gigi/multi-git/pkg/repo_manager/repo_manager.go:37.30,40.3 2 1
github.com/the-gigi/multi-git/pkg/repo_manager/repo_manager.go:45.43,47.2 1 1
github.com/the-gigi/multi-git/pkg/repo_manager/repo_manager.go:49.78,53.52 4 1
github.com/the-gigi/multi-git/pkg/repo_manager/repo_manager.go:74.2,78.28 4 1
github.com/the-gigi/multi-git/pkg/repo_manager/repo_manager.go:92.2,92.8 1 1
github.com/the-gigi/multi-git/pkg/repo_manager/repo_manager.go:53.52,54.41 1 1
github.com/the-gigi/multi-git/pkg/repo_manager/repo_manager.go:59.3,59.25 1 1
github.com/the-gigi/multi-git/pkg/repo_manager/repo_manager.go:70.3,70.45 1 1
github.com/the-gigi/multi-git/pkg/repo_manager/repo_manager.go:54.41,56.12 2 0
github.com/the-gigi/multi-git/pkg/repo_manager/repo_manager.go:59.25,60.43 1 0
github.com/the-gigi/multi-git/pkg/repo_manager/repo_manager.go:65.4,67.26 3 0
github.com/the-gigi/multi-git/pkg/repo_manager/repo_manager.go:60.43,62.13 2 0
github.com/the-gigi/multi-git/pkg/repo_manager/repo_manager.go:78.28,88.36 4 1
github.com/the-gigi/multi-git/pkg/repo_manager/repo_manager.go:88.36,90.4 1 0
This is not so user-friendly, but we can use the go cover tool to visualize the results:
$ go tool cover -html=coverage.out
This will read the coverage.out file and open a browser window where the covered lines are displayed in green and the uncovered lines are displayed in red:
Running focused tests#
One of the most useful features of Ginkgo is focusing tests. When trying to debug a problem, you often want to run just a single test case. With Ginkgo, you can add a capital F in front of any block to run just that block. The most common case is focusing It test cases, which become FIt. Here is an example. Let’s focus the following test only:
FIt("Should create branches successfully", func() {
repoList = append(repoList, "dir-2")
CreateDir(baseDir, repoList[1], true)
rm, err := NewRepoManager(baseDir, repoList, true)
Ω(err).Should(BeNil())
output, err := rm.Exec("checkout -b test-branch")
Ω(err).Should(BeNil())
for _, out := range output {
Ω(out).Should(Equal("Switched to a new branch 'test-branch'\n"))
}
})
Now when running the tests all other tests will be skipped:
$ ginkgo
Running Suite: RepoManager Suite
================================
Random Seed: 1575180041
Will run 1 of 7 specs
SSSSS•S
Ran 1 of 7 Specs in 0.294 seconds
SUCCESS! -- 1 Passed | 0 Failed | 0 Pending | 6 Skipped
PASS | FOCUSED
Ginkgo ran 1 suite in 8.549546125s
Test Suite Passed
Detected Programmatic Focus - setting exit status to 197
Note that the exit status is not zero when running focused tests. If you run Ginkgo as part of an automated CI/CD pipeline it will probably fail if you run with a focus, which is a good thing because you normally want to run all the tests you didn’t skip explicitly.
Press run on the application below. It will run the tests suits. You can try running all the commands shown in the lesson in the terminal as well.
/
Quiz#
How do you generate a coverage report?
Add the -cover flag
Add the -coverprofile=
Run the “go cover” tool
Writing BDD Tests for Multi-git
Preparing for End-to-End Tests